<?php
// А

$settings['cuteresolve_domains'] = '\.quicktorrent\.ru'; // This regular expression matches domains supporting CuteResolve
$settings['listen'] = '195.91.176.40';									// DNS-server Host
$settings['listen_port'] = 53;													// DNS-server port
$settings['pidfile'] = COREBIND_ROOT.'cutebind.pid';		// Default pid-file
$settings['use_fork'] = TRUE;														// Enables multi-threading if possible.
$settings['table'] = array(															// Simple inline resolving hash-table.
   'quicktorrent.ru.' =>
      array(
					'A' => '195.91.176.40',
					'MX' => '20=195.91.176.40',
					'NS' => 'ns1.quicktorrent.ru.',
					'SOA' => array(
							'PRIMARY_NS' => 'ns1.quicktorrent.ru.',
							'ADMIN_MB' => 'admin.quicktorrent.ru',
							'SERIAL_NUMBER' => 2008100200,
							'REFRESH_INTERVAL' => 3600,
							'RETRY_INTERVAL' => 3600,
							'EXPIRATION' => 3600,
							'MIN_TTL' => 100,
					),
	 ),					
   '195.91.176.40' => array('PTR' => 'we.are.quicktorrent.ru.'),
   'example.tld.' => array('A' => 'RR(1.2.3.4,1.2.3.5,1.2.3.6)'),
);
$settings['TTL'] = 58;																	// Time-To-Life (TTL)
$settings['worker'] = FALSE;
$settings['cutebind_path'] = 'cutebind';								// Path to CuteBind's executable file.
$settings['logging'] = 1;																// Logging level
$settings['setuser'] = '';															// You can set user of master process (sudo).
$settings['setgroup'] = '';															// You can set group of master process (sudo).

$settings['dbaddr'] = 'mysql://root:@localhost/cuteresolve';
function resolver(&$cq,$init = FALSE)
{
 global $settings;
 static $db = NULL;
 if ($db === NULL)
 {
  $p = parse_url($settings['dbaddr']);
  $db = mysql_connect($p['host'].':'.(isset($p['port'])?$p['port']:''),$p['user'],isset($p['pass'])?$p['pass']:'');
  mysql_select_db(trim($p['path'],'/'),$db);
 }
 static $geoip_isp = NULL;
 if ($geoip_isp  === NULL)
 {
  require_once "Net/GeoIP.php";
  $geoip_isp = Net_GeoIP::getInstance(COREBIND_ROOT.'GeoIPISP/GeoIPISP.dat');
 }
 if ($init) {return;}
 if ($cq['QTYPE'] != 'A') {return FALSE;}
 if (preg_match('~(?:^|\.|\-)(?:0x([\da-f]{40})[\.\-])?(?:0?x)?(?:([\da-f]{8})|(\d+\-\d+\-\d+\-\d+))[\.\-](?:0?x)?(?:([\da-f]{8})|(\d+\-\d+\-\d+\-\d+))'.$settings['cuteresolve_domains'].'~i',$cq['domain'],$m))
 {
  $peer_id = isset($m[1])?strtolower($m[1]):'';
  $m_in_ip = isset($m[2])?$m[2]:'';
  $m_in_ip_p = isset($m[3])?$m[3]:'';
  if ($m[1] !== '') {$in_ip = hexdec(substr($m_in_ip,0,2)).'.'.hexdec(substr($m_in_ip,2,2)).'.'.hexdec(substr($m_in_ip,4,2)).'.'.hexdec(substr($m_in_ip,6,2));}
  else {$in_ip = str_replace('-','.',$m_in_ip_p);}

  $m_ex_ip = isset($m[4])?$m[4]:'';
  $m_ex_ip_p = isset($m[5])?$m[5]:'';
  if ($m_ex_ip !== '') {$ex_ip = hexdec(substr($m_ex_ip,0,2)).'.'.hexdec(substr($m_ex_ip,2,2)).'.'.hexdec(substr($m_ex_ip,4,2)).'.'.hexdec(substr($m_ex_ip,6,2));}
  else {$ex_ip = str_replace('-','.',$m_ex_ip_p);}

  if ($m_peer !== '')
  {
   if ($cq['peer_ip'] == $ex_ip)
   {
    if (!mysql_query($q = 'REPLACE INTO `peers` SET `peer_id` = \''.mysql_escape_string($peer_id).'\''
     .', `ctime` = '.time()
     .', `external_ip` = '.ip2long($ex_ip)
     .', `internal_ip` = '.ip2long($in_ip)
    ,$db)) {exit(mysql_error());}
    $cq['IP'] = $in_ip;
    return TRUE;
   }
   else
   {
    $result = mysql_query('SELECT * FROM `peers` WHERE `peer_id` = \''.mysql_escape_string($peer_id).'\'');
    if ($row = mysql_fetch_assoc($result))
    {
     return $row[$result];
    }
   }
  }
  if ($cq['peer_ip'] == $ex_ip) {$cq['IP'] = $in_ip;}
  //elseif ($cq['peer_ip'] == $in_ip) {$cq['IP'] = $in_ip;}
  else
  {
   $cq['peer_ISP'] = NULL;
   $cq['ex_ip_ISP'] = NULL;
   try
   {
    $cq['peer_ISP'] = $geoip_isp->lookupOrg($cq['peer_ip']);
    $cq['ex_ip_ISP'] = $geoip_isp->lookupOrg($cq['ex_ip']);
   } catch (Exception $e) {}
   if (($cq['peer_ISP'] === NULL) || ($cq['ex_ip_ISP'] === NULL))
   {
    $ex_host = gethostbyaddr($ex_ip);
    $cq['peer_host'] = gethostbyaddr($cq['peer_ip']);
    $ex_host_a = explode('.',strtolower($ex_host));
    $s = sizeof($ex_host_a);
    $peer_host_a = explode('.',strtolower($cq['peer_host']));
    if (($cq['peer_host'][$s-2] == $ex_host_a[$s-2]) && ($peer_host_a[$s-1] == $ex_host_a[$s-1]))
    {
     $cq['IP'] = $cq['in_ip'];
    }
    else {$cq['IP'] = $cq['ex_ip'];}
   }
   elseif ($cq['peer_ISP'] == $cq['ex_ip_ISP']) {$cq['IP'] = $cq['in_ip'];}
   else {$cq['IP'] = $cq['ex_ip'];}

   var_dump($cq['peer_ISP']);
   var_dump($cq['ex_ip_ISP']);
  }
  return TRUE;
 }
 return FALSE;
}